<template>
  <el-card shadow="hover">
    <el-form
      ref="CategorySelectorForm"
      :model="CategorySelectorForm"
      label-width="120px"
      inline
    >
      <el-form-item label="一级分类">
        <el-select placeholder="请选择" v-model="category1Id">
          <el-option :label="c1.name" :value="c1.id" v-for="c1 in category1List"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select placeholder="请选择" v-model="category2Id">
          <el-option :label="c2.name" :value="c2.id" v-for="c2 in category2List"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select placeholder="请选择" v-model="category3Id">
          <el-option :label="c3.name" :value="c3.id" v-for="c3 in category3List"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import {
  getCategory1ListApi,
  getCategory2ListApi,
  getCategory3ListApi,
} from "@/api/product/category";
export default {
  name: "CategorySelector",
  data() {
    return {
      CategorySelectorForm: {
        // category1,
        // category2,
        // category3,
      },
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,
      category1List:[],
      category2List:[],
      category3List:[],

    };
  },
  async mounted() {
    const res = await getCategory1ListApi()
    // console.log(res);
    this.category1List = res.data
  },
  watch:{
    async category1Id(category1Id,oldVal){

      // console.log(newVal,oldVal);
      this.category2Id = undefined
      this.category3Id = undefined
      const res = await getCategory2ListApi(category1Id)
      console.log(res);
      this.category2List = res.data
    },
    async category2Id(category2Id,oldVal){
      if(!category2Id) return
      this.category3Id = undefined
      const res = await getCategory3ListApi(category2Id)
      console.log(res);
      this.category3List = res.data
    }
  }
};
</script>

<style scoped>
::v-deep .el-form-item {
  margin-top: 10px;
  margin-bottom: 10px;
}
</style>
